package com.bob.light.activity

import android.content.Intent
import android.os.Bundle
import android.widget.CheckBox
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.bob.light.R
import com.bob.light.TuyaHomeSDKHelper
import com.bob.light.config.AppConfig
import com.bob.light.config.DeviceConfig
import com.bob.light.setOnSingleClickListener
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.tuya.smart.sdk.bean.DeviceBean
import kotlinx.android.synthetic.main.app_activity_device_manager.*
import kotlinx.android.synthetic.main.app_title_bar.*

class DeviceManagerActivity : AppCompatActivity() {

    private var mAdapter: BaseQuickAdapter<DeviceBean, BaseViewHolder>? = null

    companion object {

        @JvmStatic
        fun navigation(activity: AppCompatActivity?) {
            activity?.let { context ->
                val intent = Intent(context, DeviceManagerActivity::class.java)
                context.startActivity(intent)
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.app_activity_device_manager)
        initView()
        initData()
    }

    private fun initView() {
        fl_back_pressed?.setOnSingleClickListener {
            finish()
        }
        tv_title_name?.text = title
        initRecyclerView()
    }

    private fun initData() {
        val devices = DeviceConfig.queryDeviceList()
        mAdapter?.setNewData(devices)
    }

    private fun initRecyclerView() {
        recycler_item_device?.let { recyclerView ->
            recyclerView.layoutManager = LinearLayoutManager(this@DeviceManagerActivity)
            mAdapter = initAdapter { isChecked, item ->
                TuyaHomeSDKHelper.toggleDeviceState(item.devId, isChecked)
            }
            recyclerView.adapter = mAdapter
        }
    }

    val mainColor = ContextCompat.getColor(AppConfig.application(), R.color.app_color_main)
    val hintColor = ContextCompat.getColor(AppConfig.application(), R.color.app_color_ff999999)

    private fun initAdapter(block: ((isChecked: Boolean, item: DeviceBean) -> Unit)? = null)
            : BaseQuickAdapter<DeviceBean, BaseViewHolder> {
        return object : BaseQuickAdapter<DeviceBean, BaseViewHolder>(R.layout.app_item_device) {
            override fun convert(helper: BaseViewHolder?, item: DeviceBean?) {
                if (item == null) {
                    return
                }

                helper?.getView<TextView>(R.id.tv_device_name)?.text = item.name
                val cb = helper?.getView<CheckBox>(R.id.cb_device_toggle)
                if (item.dps != null && (item.dps["1"] != null)) {
                    cb?.isChecked = item.dps["1"] as Boolean
                } else {
                    cb?.isChecked = true
                }
                cb?.setTextColor(if (cb.isChecked) mainColor else  hintColor)
                cb?.setOnCheckedChangeListener { _, isChecked ->
                    cb.setTextColor(if (isChecked) mainColor else  hintColor)
                    block?.invoke(isChecked, item)
                }
            }
        }
    }

}